package com.ugrow.ugrowcommunity.vm.schedule;

import android.annotation.SuppressLint;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.GridLayoutManager;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ugrow.ugrowcommunity.BuildConfig;
import com.ugrow.ugrowcommunity.data.Data;
import com.ugrow.ugrowcommunity.R;
import com.ugrow.ugrowcommunity.databinding.ScheduleFragmentBinding;
import com.ugrow.ugrowcommunity.util.HttpUtil;
import com.ugrow.ugrowcommunity.vm.base.BaseFragment;

import java.io.IOException;
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

public class ScheduleFragment extends BaseFragment<ScheduleFragmentBinding,ScheduleViewModel> {

    // binding 和 mViewModel 可以直接使用,无需声明;


    private List<ClassBean> list = new ArrayList<>();


    public static ScheduleFragment newInstance() {
        return new ScheduleFragment();
    }

    @Override
    protected ScheduleFragmentBinding setContentView(LayoutInflater inflater, @Nullable ViewGroup container) {
        return ScheduleFragmentBinding.inflate(inflater, container, false);
    }

    @Override
    protected ScheduleViewModel setViewModel() {
        return ViewModelProviders.of(this).get(ScheduleViewModel.class);
    }

    @Override
    protected void onVisible() {
        //每次加载时执行
    }

    @Override
    protected void onFirstLoad() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("major", "软件工程");
        HttpUtil.httpPost(BuildConfig.COURSE_TABLE, map, HttpUtil.ContentType.JSON, new HttpUtil.CallBack() {
            @Override
            public void success(HttpUtil.Result result) {
                Log.d("TAG", "success: " + result.content);
                Type type = new TypeToken<ArrayList<ClassBean>>() {
                }.getType();
                list = new Gson().fromJson(result.content, type);
                GridLayoutManager gridLayoutManager = new GridLayoutManager(requireActivity(), 5);
                binding.recyclerview.setLayoutManager(gridLayoutManager);
                binding.recyclerview.setAdapter(new ClassAdapter(requireContext(), list));
                binding.recyclerview.setLayoutAnimation(AnimationUtils.loadLayoutAnimation(getContext(), R.anim.item_anim));
            }

            @Override
            public void failed(IOException e) {
                Toast.makeText(requireActivity(), "课表获取失败，请稍后重试！", Toast.LENGTH_SHORT).show();
            }
        });
        //显示日期
        List<String> list = getWeekDay();
        binding.week1.setText(list.get(1));
        binding.week2.setText(list.get(2));
        binding.week3.setText(list.get(3));
        binding.week4.setText(list.get(4));
        binding.week5.setText(list.get(5));
        binding.month.setText(list.get(7));
    }

    @SuppressLint("SimpleDateFormat")
    private static List<String> getWeekDay() {
        Calendar calendar = Calendar.getInstance();
        int firstDayOfWeek = calendar.getFirstDayOfWeek();
        List<String> list = new ArrayList<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd");
        for (int i = 0; i < 7; i++) {
            calendar.set(Calendar.DAY_OF_WEEK, firstDayOfWeek + i);
            list.add(simpleDateFormat.format(calendar.getTime()));
        }
        list.add(String.valueOf(calendar.get(Calendar.MONTH)+1));
        return list;
    }
}